package com.ch04.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ch04.service.AppInfoVOService;
import com.ch04.vo.AppInfoVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * @author Jane
 * @date 2024-05-31 17:41
 */

@Controller
@RequestMapping("/devuser")
public class DevUserController {

    @Resource
    AppInfoVOService appInfoVOService;

    @RequestMapping("/tomain")
    public String tomain(Model model
                        ,@RequestParam(name = "softwarename",required = false) String softwarename
                        ,@RequestParam(name = "flatformName",required = false) Long flatformId
                        ,@RequestParam(name = "status",required = false) Long status
    ){

        //条件查询
        AppInfoVO appinfo=  new AppInfoVO();

        appinfo.setSoftwarename(softwarename);
        appinfo.setStatus(status);
        appinfo.setFlatformid(flatformId);

        if (flatformId!=null && flatformId>0){
            model.addAttribute("fid",flatformId.intValue());
        }
        if (status!=null && status>0){
            model.addAttribute("_status",status.intValue());
        }

        //分页
        Page<AppInfoVO> page= new Page<>(1,5);
        Page<AppInfoVO> pageObj = appInfoVOService.getAppinfoList(page, appinfo);

        model.addAttribute(pageObj);
        //数据集合
        model.addAttribute("appList",pageObj.getRecords());

        return "/dev/main";  // templates/main.html
    }

    @RequestMapping("/to_mainfragment")
    public String to_mainfragment(Model model
            ,@RequestParam(name = "softwarename",required = false) String softwarename
            ,@RequestParam(name = "flatformName",required = false) Long flatformId
            ,@RequestParam(name = "status",required = false) Long status
            ,@RequestParam(name = "pageIndex",required = false,defaultValue = "1") Integer pageIndex

    ){
        //条件查询
        AppInfoVO appinfo=  new AppInfoVO();

        appinfo.setSoftwarename(softwarename);
        appinfo.setStatus(status);
        appinfo.setFlatformid(flatformId);

        if (flatformId!=null && flatformId>0){
            model.addAttribute("fid",flatformId.intValue());
        }
        if (status!=null && status>0){
            model.addAttribute("_status",status.intValue());
        }
        //分页
        Page<AppInfoVO> page= new Page<>(pageIndex,5);
        Page<AppInfoVO> pageObj = appInfoVOService.getAppinfoList(page, appinfo);

        model.addAttribute("pageObj",pageObj);

        System.out.println("共"+ pageObj.getPages() + "页");
        System.out.println("共"+ pageObj.getTotal() + "条");
        System.out.println("当前第"+ pageObj.getCurrent()+ "页");
        System.out.println("每页"+pageObj.getSize()+"条");
        //数据集合
        model.addAttribute("appList",pageObj.getRecords());

        return "/dev/main_fragment";
    }
}
